<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
<style>
.asbutton {
	display: block;
	width:100px; background:#ccc; padding: 3px;
}
.adsearch {
	float: right; width: 300px;
	padding: 20px 10px; background: #fff; border: 1px solid #ccc;
	margin-bottom: 10px;
}
</style>
<!--domain tooltip -->
<div class="ttip-ad-preview" id="tooltip2">
    <p class="myclear">
		<img alt="" class="ttip-ad-preview-arrow" src="/images/toannhtooltiparrow.gif" />
	</p>
	<div class="trung-ttip-ad-preview-inner1">
		<div class="ttip-ad-preview-inner-pad" id="content2">
            <div style="width: 400px;height:246px;text-align: center;">
				<img src="/images/ajax-loader.gif" style="margin-top: 80px;"/>
            </div>
		</div>
		<p class="ttip-adprv-btn-box myclear">
            <a onclick="assign_campaign()" class="btn marright10">
                <span>
				    <?php echo 'assign';?>
                </span>
			</a>
			<a onclick="domain_tooltip_preview_remove()" class="btn">
                <span>
				    <?php echo 'cancelTooltip';?>
                </span>
			</a>
		</p>
	</div>
</div>
<!--end domain tooltip -->
<div class="middle myclear">
	<div class="pad10">
		
		<h3><?php echo $user_lang['module_title']; ?></h3>
				
		<table class="martop10 marbottom10" width="100%">
			<tr>
				<td class="ver-middle">
					<b class="ver-middle marright5">Filter by: </b>
					<select id="slcType" class="ver-middle" onchange="usersFilterChange();">
						<option value="active" <?php echo ($type == 'active') ? 'selected="selected"' : '';?> >Active user</option>
						<option value="unactive" <?php echo ($type == 'unactive') ? 'selected="selected"' : '';?> >UnActive user</option>
						<option value="delete" <?php echo ($type == 'delete') ? 'selected="selected"' : '';?> >Deleted user</option>
						<option value="block" <?php echo ($type == 'block') ? 'selected="selected"' : '';?> >Block user</option>
					</select>
				</td>
				
				<td class="ver-middle">
					<b class="ver-middle marright5">Privilege: </b>
					<select id="slcLevel" class="ver-middle" onchange="usersFilterChange();">
						<option value="-2">All</option>
						<?php foreach ($lang['arr_role'] as $val=>$key){?>
                        <option value="<?php echo $val;?>" <?php if($level==$val) echo 'selected="selected"'?>><?php echo $key;?></option>
                        <?php }?>
					</select> &nbsp;
					
				</td>
				
				<td class="ver-middle">
				<b class="ver-middle marright5">Search: </b>
				<input type="text" name="keyword" id="txtKeyword" size="20" value="<?php echo $this->input->get('keyword',true)?>"></input>
				<input type="button" name="btnSearch" value="Search" onclick="usersFilterChange()"></input>
				</td>
				
				<td class="ver-middle" style="text-align:right">
					<input type="button" onclick="window.location.href='<?php echo site_url('user/create')?>'" value="Tạo người dùng" />
				</td>
			</tr>
		</table>
		<h3>
			Danh sách user:
		</h3>
		
		<table width="100%" border="1" class="martop10 tbl-list">
			<tr>
				<td class="pad5 text-center td-label" width="40">
					#
				</td>
				
				<td class="pad5 td-label" width="130" >
					<b>Username</b>
				</td>
				
				<td class="pad5 td-label">
					<b>Fullname</b>
				</td>
								
				<td class="pad5 td-label" >
					<b>User level</b>
				</td>
				
				
				<td class="pad5 td-label" width="120" >
					<b>Actions</b>
				</td>
                
                <td class="pad5 td-label text-center" width="50" >
					<b>Assigns</b>
				</td>
			</tr>
			
			<?php
			foreach($lstUser as $user)
			{
			?>
				<tr class="row">
					<td class="pad5 text-center" >
						<?php echo $user['user_id']; ?>
					</td>
					
					<td class="pad5">
						<a href="<?php echo site_url('user/edit/'.$user['user_id'])?>">
							<?php echo $user['username']; ?>
						</a>
					</td>
					
					<td class="pad5">
						<?php echo $user['truename']; ?>
					</td>
									
					<td class="pad5"  >
						<?php
						if($user['permitID'] >= 3 ) echo $lang['sale'];
						else echo array_key_exists($user['permitID'], $user_lang['arr_role']) ? $user_lang['arr_role'][$user['permitID']] : '';
						?>
					</td>
					
					<td class="pad5 text-center" >
						<?php
						if($type == 'active')
						{
						?>
							<select id="slcAction_<?php echo $user['user_id']?>" style="width: 60px;" >
								<option value="block">Block</option>
								<option value="delete">Delete</option>
								<?php
								if($user['permitID'] == 3)
								{
								?>
									<option value="publisher">Set is publisher</option>
									<option value="agency">Set is Agency</option>
								<?php
								}
								else if($user['permitID'] == 2)
								{
								?>
									<option value="publisher">Set is publisher</option>
									<option value="user">Set is User</option>
								<?php
								}
								else if($user['permitID'] == 1)
								{
								?>
									<option value="agency">Set is Agency</option>
									<option value="user">Set is User</option>
								<?php
								}
								?>
							</select>
						<?php
						}
						else if($type == 'unactive')
						{
						?>
							<select id="slcAction_<?php echo $user['user_id']?>" style="width: 115px;">
								<option value="active">Active</option>
							</select>
						<?php
						}
						else if($type == 'block')
						{
						?>
							<select id="slcAction_<?php echo $user['user_id']?>" style="width: 115px;">
								<option value="unblock">UnBlock</option>
							</select>
						<?php
						}else if($type == 'delete')
						{
						?>
							<select id="slcAction_<?php echo $user['user_id']?>" style="width: 115px;">
								<option value="undelete">UnDelete</option>
							</select>
						<?php
						}
						?>
						
						<input  type="button" value="Submit" onclick="userAction('<?php echo site_url('user/action'); ?>', '<?php echo $user['user_id']; ?>');" />
					</td>
                    <td class="pad5 text-center"  >
                    	<?php if(($user['permitID']==4)&&($type == 'active')){ ?>
						<a onclick="ajax_tooltip2('<?php echo $user['user_id'];?>',this)"><?php echo 'assign';?></a>
						<?php } else echo '&nbsp;';?>
					</td>
				</tr>
			<?php
			}
			?>
			
		</table>
		
		<?php
		if($numCurrItem > 0)
		{
		?>
			<table width="100%" class="martop10">
				<tr>
					<td class="pad5">
						Show <?php echo $cPage; ?> / <?php echo $pCount; ?> pages
					</td>
					
					<td class="pad5 text-right ppage">
						<?php
						if($cPage > 1)
						{
							?>
							<a href="<?php echo site_url('user/view')."?type=$type&level=$level&keyword=$keyword&cmofsale=$cmofsale&cpage=" . ($cPage - 1); ?>">Previous</a>
							<?php
						}
						
						foreach($lstPaging as $page)
						{
							if($page > 0)
							{
						?>
							<a <?php echo ($page == $cPage) ? 'class="curr"' : '';?> href="<?php echo site_url('user/view')."?type=$type&level=$level&cpage=$page&keyword=$keyword"; ?>"><?php echo $page;?></a>
						<?php
							}
						}
						
						if($cPage < $pCount)
						{
							?>
							<a href="<?php echo site_url('user/view')."?type=$type&level=$level&keyword=$keyword&cmofsale=$cmofsale&cpage=" . ($cPage + 1); ?>">Next</a>
							<?php
						}
						?>
					</td>
				</tr>
			</table>
		<?php
		}
		?>
	</div>
</div>
<input type="hidden" id="assign"/>
<script type="text/javascript">
	$(function(){
		$('.tbl-list .row').mouseover(function(){
			$(this).css('background','#ffc');
		});
		$('.tbl-list .row').mouseout(function(){
			$(this).css('background','none');
		});
	});
	function usersFilterChange()
	{
		var type = $('#slcType').val();
		var level = $('#slcLevel').val();
		var group = $('#slcGroup').val();
		var keyword = $('#txtKeyword').val();
		var cmofsale = $('input[name="cmofsale"]').val();
		
		window.location = '<?php echo site_url('user/view/'); ?>?type=' + type + '&level=' + level + '&keyword=' + keyword;
	}
    function assign_campaign()
    {
        var value = $('#empty-array-form').find('select').val();
        var lstCampaign = value;
        var userid = $('#assign').val();
        //alert(lstCampaign);
        var strURL="<?php echo site_url('user/assign?userid=');?>"+userid+"&lst="+lstCampaign;
        var success = function(data)
        {
            alert(data);
            domain_tooltip_preview_remove();
        };
        getAjax(strURL,"",'', '', '', false, success);
    }
	function ajax_tooltip2(val,e)
    {    
        var strURL="<?php echo site_url('user/get_list_campaign?userid=');?>"+val;
        $('#assign').val(val);
        var strId= '#tooltip2';
        var strId2='#content2';
        tooltip_preview (e,strId);
        var success = function(data)
        {
            $(strId2).html(data);
            tooltip_preview (e,strId);
        };
        getAjax(strURL,"",'', '', '', false, success);
    }
    function getLstid()
    {
        var total="";
        var x=document.getElementsByName("lstidCheckbox");
        for(var i=0; i < x.length; i++)
        {
            if(x[i].checked)
            {
                total +=x[i].value + ",";
            }
        }
        var t=total.replace(/,$/,"");
        $('#lstid').val(t);
    }
    function domain_tooltip_preview_remove()
    {
        tooltip_preview_remove('.ttip-ad-preview');
        $('#content2').html('<div style="width: 300px;height:246px;text-align: center;"><img src="/images/ajax-loader.gif" style="margin-top: 80px;"/></div>');
    }
	function userAction(url, id)
	{
		var action = $('#slcAction_'+id).val();
		var onSuccess = function(data)
		{
			var objData = jQuery.parseJSON(data);
			if(objData.code == 1)
			{
				alert('Update thành công');
				window.location.reload(true);
			}
			else
			{
				alert('Update lỗi');
			}
		};
		getAjax(url, 'action=' + action + '&id=' + id, '', '', '', false, onSuccess);
	}
	function adSearch()
	{
		$('.adsearch').animate({
		    opacity: 1,
		    height: 'toggle'
		 });
		//$('.asbutton').toggleClass('asbutton2');
	}
</script>